package sh.wn36.vehiclemanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import sh.wn36.vehiclemanage.entity.*;
import sh.wn36.vehiclemanage.service.*;
import sh.wn36.vehiclemanage.util.Result;
import sh.wn36.vehiclemanage.xzy.InsuranceInfo;
import sh.wn36.vehiclemanage.xzy.Query;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xk
 * @since 2021-04-26
 */
@Api("保险记录")
@CrossOrigin
@RestController
@RequestMapping("/insurance-record")
public class InsuranceRecordController {

    @Autowired
    private InsuranceRecordService recordService;
    @Autowired
    private CarService carService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private CurrentUnitService currentUnitService;
    @Autowired
    private DriverService driverService;

    @ApiOperation("根据条件分页查询")
    @PostMapping("/pageUnitByConditon/{current}/{limit}")
    public Result pageDriverByConditon(@ApiParam(value = "当前页码", required = true, name = "currentPage") @PathVariable("current") Integer current,
                                       @ApiParam(value = "当前页数", required = true, name = "pageSize") @PathVariable("limit") Integer limit,
                                       @ApiParam(value = "查询条件", required = false, name = "insuranceQuery") @RequestBody(required = false) Query query) {
        Page<InsuranceInfo> page = new Page<>(current, limit);

        QueryWrapper<InsuranceInfo> wrapper = new QueryWrapper<>();

        String carNo = query.getCarNo();

        if (!StringUtils.isEmpty(carNo)) {
            wrapper.like("car.car_no", carNo);
        }
        recordService.getAll(page, wrapper);
        long total = page.getTotal();

        List<InsuranceInfo> records = page.getRecords();

        return Result.OK().data("total", total).data("records", records);
    }

    @ApiOperation("添加保险记录")
    @PostMapping("/saveInsurance")
    public Result saveInsurance(@RequestBody InsuranceInfo insuranceInfo) {

        boolean save = recordService.save(insuranceInfo);
        if (save)
            return Result.OK();
        else
            return Result.Fail();
    }

    @ApiOperation("删除保险记录")
    @GetMapping("/delIns/{ids}")
    public Result delIns(@ApiParam(value = "保险编号", required = true, name = "ins_id") @PathVariable List<Integer>ids) {
        boolean b = recordService.removeByIds(ids);
        if (b) {
            return Result.OK();
        } else {
            return Result.Fail();
        }
    }

    @ApiOperation("修改保险记录")
    @PostMapping("/updateIns")
    public Result updateIns(@RequestBody InsuranceInfo insuranceInfo) {
        boolean b = recordService.updateById(insuranceInfo);
        if (b) {
            return Result.OK();
        } else {
            return Result.Fail();
        }
    }

    @ApiOperation(("根据id查找保险记录"))
    @GetMapping("/findInsById/{id}")
    public Result findUnitById(@ApiParam(value = "保险编号", required = true, name = "id") @PathVariable("id") Integer id) {
        InsuranceRecord record = recordService.getById(id);
        System.out.println(record);
        return Result.OK().data("record", record);
    }

    @ApiModelProperty("查找所有关联信息")
    @PostMapping("/findAll")
    public Result findAll() {
        List<Car> carList = carService.list();
        List<Dictionary> dictionaries = dictionaryService.listByIdInsu();
        List<CurrentUnit> currentUnits = currentUnitService.listByIdInsu();
        List<Driver> driverList = driverService.list();
        return Result.OK().data("carList", carList).data("dictionaries", dictionaries).data("currentUnits", currentUnits).data("driverList", driverList);

    }
    @ApiOperation("报表接口")
    @GetMapping("/reportList")
    public Result reportList(){
        return Result.OK().data("reportList",recordService.reportList());
    }
    @ApiOperation("报表按司机ID查询接口")
    @GetMapping("/queryReportByCarId/{carId}")
    public Result queryReportByCarId(@ApiParam("车辆ID") @PathVariable Integer carId){
        return Result.OK().data("reportList",recordService.queryReportByCarId(carId));
    }
}

